using UnityEngine;
using System.Collections.Generic;
using System.Collections;
//public class MosterHitLab{
//	public float _time;
//	public UILabel _hplabel;
//	public MosterHitLab(UILabel label){
//		_hplabel = label;
//	}
//	public void Show(Vector3 pos,int value){
//		_time = 0;
//		_hplabel.gameObject.SetActive (true);
//		_hplabel.transform.parent.position = pos;
//		_hplabel.text = value.ToString ();
//	}
//	public void Hide(){
//		_time = 0;
//		_hplabel.gameObject.SetActive (false);
//	}
//	public bool UpdateTime(float dt){
//		_time += dt;
//		if (_time > 2)
//			return true;
//		else
//			return false;
//	}
//}
//public class Main_View : ViewUI{ 
//	UILabel _buildCount;
//	UILabel _roundCount;
//	UILabel _playerGold;
//	UILabel _playerHP;
//	UILabel _timeScale;
//	UISlider _playerHPSlider;
//
//	UIFollow _follow;
//	GameObject _sureBuildBtn;
//	Transform _followParent;
//	GameObject _hitHp;
//	Dictionary<Transform,UISlider> _mosterHP = new Dictionary<Transform, UISlider>();
//	List<MosterHitLab> _mosterHitHPPool = new List<MosterHitLab>();
//	List<MosterHitLab> _mosterHitHp = new List<MosterHitLab>();
//	View_StoneInfo _stoneInfo;
//	public override  void Open(){
//		_buildCount = GetUILabel ("Lab_BuildCount");
//		_roundCount = GetUILabel ("Lab_RoundCount");
//		_playerGold = GetUILabel ("Lab_Gold");
//		_playerHP = GetUILabel ("Lab_HP");
//		_hitHp = GetObj().GetGameObject ("lab_hithp");
//		_hitHp.gameObject.SetActive (false);
//
//		_playerHPSlider = GetUISlider ("Slider_HP");
//
//		_followParent = GetObj ().GetTransform ("FollowParent");
//		_sureBuildBtn = GetObj ().GetGameObject ("Btn_SureBuild");
//		_stoneInfo = GetObj ().GetGameObject ("StoneInfo").GetComponent<View_StoneInfo>();
//		_timeScale = GetObj ().GetGameObject ("Lab_TimeScale").GetComponent<UILabel> ();
//		HideBtn_SureBuild ();
//		_follow = new UIFollow ();
//
////		OnRestUIView ();
//	}
//	public override  void Close<Main_View>(){
//		base.Close<Main_View>();
//	}
//	#region UILabel
//	public void SetLabel_MosterHitHp(Transform target,int value){
//		if (_mosterHitHPPool == null || _mosterHitHPPool.Count == 0) {
//			UILabel lab;
//			lab = ObjManager.Instance.AddChildByPool (_hitHp, _followParent,2).GetComponentInChildren<UILabel>();
//			_mosterHitHPPool.Add (new MosterHitLab (lab));
//		}
//		MosterHitLab hitlab = _mosterHitHPPool[0];
//		_mosterHitHPPool.RemoveAt (0);
//		_mosterHitHp.Add (hitlab);
//		TweenBase.PlayAllTween (hitlab._hplabel.gameObject);
//		hitlab.Show(_follow.GetFollowPoint (target),value);
//	}
//	public void SetLabel_Gold(int gold){
//		_playerGold.text = gold.ToString ();
//	}
//	public void UpdateStoneChooseInfo(UIStoneInfoData stone){
//		if (stone!=null && _stoneInfo.gameObject.activeSelf) {
//			_stoneInfo.UpdateLvUpNeedGold (stone);
//		}
//	}
//	public void SetLabel_HP(int hp){
//		_playerHP.text = hp.ToString ();
//	}
//
//	public void SetLabel_RoundCount(int count){
//		_roundCount.text = count.ToString ();
//	}
//	public void SetLabel_BuildCount(int count){
//		_buildCount.text = count.ToString ();
//	}
//	public void SetLabel_TimeScale(int timescale){
//		_timeScale.text = "时间x"+timescale;
//	}
//	#endregion
//	#region Slider
//	public void SetLabel_Slider(float value){
//		_playerHPSlider.value = value;
//	}
//	#endregion
//	public void HideBtn_GameStart(){
////		SetActive ("GameStart", false);
//		NGUITools.SetActive (GetObj ().GetGameObject ("GameStart"), false);
//	}
//	public void ShowBtn_GameStart(){
////		SetActive ("GameStart", true);
//		NGUITools.SetActive (GetObj ().GetGameObject ("GameStart"), true);
//	}
//	public void ShowBtn_SureBuild(){
//		if (_sureBuildBtn.gameObject.activeSelf == false)
//			_sureBuildBtn.gameObject.SetActive (true);
//	}
//	public void HideBtn_SureBuild(){
//		if(_sureBuildBtn!=null)
//			_sureBuildBtn.gameObject.SetActive (false);
//	}
//	public void ShowPanel_StoneInfo(UIStoneInfoData stone){
//		if (_stoneInfo != null) {
//			_stoneInfo.gameObject.SetActive (true);
//			_stoneInfo.ShowStoneInfo (stone);
//		}
//	}
//	public void HidePanel_StoneInfo(){
//		if (_stoneInfo != null) {
//			_stoneInfo.OnHide ();
//		}
//	}
//	public void Add_BtnTimeScale(UIEventListener.VoidDelegate callback){
//		AddButtonListener ("Btn_TimeScale",callback);
//	}
//
//	public void Add_BGCollider(UIEventListener.VoidDelegate callback){
//		AddButtonListener ("BGCollider", callback);
//	}
//	public void Add_SureBuildStone(UIEventListener.VoidDelegate callback){
//		AddButtonListener ("Btn_SureBuild", callback);
//	}
//	public void Add_GameStart(UIEventListener.VoidDelegate callback){
//		AddButtonListener ("GameStart", callback);
//	}
//	public void Add_BtnBuild(UIEventListener.VoidDelegate callBack){
//		AddButtonListener ("Btn_Build", callBack);
//	}
//	public void Add_BtnFit(UIEventListener.VoidDelegate callBack){
//		_stoneInfo.Add_BtnFit (callBack);
//	}
//	public void Add_BtnFitEx(UIEventListener.VoidDelegate callBack){		
//		_stoneInfo.Add_BtnFitEx (callBack);
//	}
//	public void Add_BtnStoneReserve(UIEventListener.VoidDelegate callBack){
//		_stoneInfo.Add_BtnStoneReserve (callBack);
//	}
//	public void Add_BtnStoneLvUp(UIEventListener.VoidDelegate callBack){
//		_stoneInfo.Add_BtnStoneLvUp (callBack);
//	}
//	public void LateUpdate(){
//		_follow.LateUpdateFollow ();
//		MosterHitLab hitlab;
//		for (int i = 0; i < _mosterHitHp.Count; i++) {
//			hitlab = _mosterHitHp [i];
//			if (hitlab.UpdateTime (Time.deltaTime)) {
//				hitlab.Hide ();
//				_mosterHitHp.RemoveAt (i);
//				_mosterHitHPPool.Add (hitlab);
//				i++;
//			}
//		}
//	}
//	public void RemoveFollowData(Transform follow){
//		_follow.Remove (follow);
//	}
//	public UIFollowData AddFollowData(string name,Transform target){
//		GameObject newobj = ObjManager.Instance.AddChildByPool(GetObj().GetGameObject(name),_followParent);
//		newobj.transform.localScale = Vector3.one;
//		newobj.transform.localPosition = Vector3.one * 1000;
//		newobj.SetActive (true);
//		UIFollowData data;
//		data = _follow.Add(target,newobj.transform);
//		Vector3 offset = new Vector3(0,2,-0.8f);
//		data._offset = offset;
//		return data;
//	}
//	private void AddFollowHP(Transform target){
//		if (!_mosterHP.ContainsKey (target)) {
//			GameObject newobj = ObjManager.Instance.AddChildByPool(GetObj().GetGameObject("follow_hp"),_followParent);
//			newobj.transform.localScale = Vector3.one;
//			newobj.transform.localPosition = Vector3.one * 1000;
//	
//			UISlider slider = newobj.GetComponent<UISlider>();
//			_mosterHP.Add(target,slider);
//			Vector3 offset = new Vector3(0,2,0);
//			UIFollowData data;
//			data = _follow.Add(target,newobj.transform);
//			data._offset = offset;
//		}
//	}
//
//
//	public void UpdateMosterHP(Transform target,float value){
//		if (!_mosterHP.ContainsKey (target))
//			AddFollowHP (target);
//		_mosterHP [target].value = value;
//	}
//	public void RemoveFollowHP(Transform  target){
//		_follow.Remove (target);
//		if (_mosterHP.ContainsKey (target)) {
//			_mosterHP.Remove (target);
//		}
//	}
//
//	public void ShowBuilding(){
//		SetActive ("Building", true);
//		SetActive ("Choose", false);
//		SetActive ("MosterAttack", false);
//	}	
//	public void ShowChooseStone(){
//		SetActive ("Building", false);
//		SetActive ("Choose", true);
//		SetActive ("MosterAttack", false);
//	}
//	public void ShowMosterAttack(){	
//		SetActive ("Building", false);
//		SetActive ("Choose", false);
//		SetActive ("MosterAttack", true);
//	}
//	public void OnGameOver(){
//		Close<Main_View>();
//	}
//}
[System.Serializable]
public class UIFollow{
	public Camera _cameraScene;
	public Camera _cameraUI {
		get {
			if (m_cameraUI == null) {
				m_cameraUI = GameObject.Find ("UICamera2D").GetComponent<Camera> ();
			}
			return m_cameraUI;
		}
	}
	Camera m_cameraUI;
	public UIFollow(){
		_cameraScene = Camera.main;
	}
	public Dictionary<Transform,UIFollowData> _followdata = new Dictionary<Transform, UIFollowData>();
	public List<UIFollowData> _listdata = new List<UIFollowData> ();
	public UIFollowData Add(Transform target,Transform follow){
		if(!_followdata.ContainsKey(target)){
			UIFollowData data = new UIFollowData(follow,target);
			_followdata.Add(target,data);
			_listdata.Add(data);
		}
		return _followdata[target];
	}
	public void Remove(Transform target,bool removeFollowObj = true){
		if(_followdata.ContainsKey(target)){
			if(removeFollowObj)
			_followdata[target].OnRemove();
			_listdata.Remove(_followdata[target]);
			_followdata.Remove(target);
		}
	}

	public void LateUpdateFollow(){
		for(int i = 0;i < _listdata.Count;i++){
			_listdata[i].LateUpdate(_cameraScene,_cameraUI);
		}
	}
	public Vector3 GetFollowPoint(Vector3 target){
		Vector3 screenpos;
		Vector3 viewpos;
		screenpos = _cameraScene.WorldToScreenPoint (target);
		viewpos = _cameraUI.ScreenToWorldPoint (new Vector3 (screenpos.x,screenpos.y,_cameraUI.nearClipPlane));
		viewpos.z = 0;
		return viewpos;
	}
	public Vector3 GetFollowPoint(Transform target){
		return GetFollowPoint (target.position);
	}
}
[System.Serializable]
public class UIFollowData{
	public Transform _followObj;
	public Transform _targetObj;
	public Vector3 _offset;
	public Vector3 tarpos;
	public Vector3 screenpos;
	public Vector3 viewpos;
	public UIFollowData(Transform followObj,Transform target){
		_followObj = followObj;
		_targetObj = target;
	}
	public void OnRemove(){
		ObjManager.Instance.Despawn (_followObj.gameObject);
	}
	public void LateUpdate(Camera cameraScene,Camera cameraUI){

		tarpos = _targetObj.position;
		tarpos.x += _offset.x;
		tarpos.y += _offset.y;
		tarpos.z += _offset.z;
		screenpos = cameraScene.WorldToScreenPoint (tarpos);
		viewpos = cameraUI.ScreenToWorldPoint (new Vector3 (screenpos.x,screenpos.y,cameraUI.nearClipPlane));
		viewpos.z = 0;
		_followObj.position = viewpos;
	}
}